package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct3Pane extends GDAbstractRiftPane {
    private JCheckBox cbKurastDocks;
    private JCheckBox cbSpiderForest;
    private JCheckBox cbGreatMarsh;
    private JCheckBox cbFlayerJungle;
    private JCheckBox cbKurastLower;
    private JCheckBox cbKurastBazaar;
    private JCheckBox cbKurastUpper;
    private JCheckBox cbTravincal;
    private JCheckBox cbDuranceHate;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct3Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbKurastDocks).addComponent(this.cbSpiderForest).addComponent(this.cbGreatMarsh).addComponent(this.cbFlayerJungle).addComponent(this.cbKurastLower).addComponent(this.cbKurastBazaar).addComponent(this.cbKurastUpper).addComponent(this.cbTravincal).addComponent(this.cbDuranceHate));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbKurastDocks)).addGroup(layout.createParallelGroup().addComponent(this.cbSpiderForest)).addGroup(layout.createParallelGroup().addComponent(this.cbGreatMarsh)).addGroup(layout.createParallelGroup().addComponent(this.cbFlayerJungle)).addGroup(layout.createParallelGroup().addComponent(this.cbKurastLower)).addGroup(layout.createParallelGroup().addComponent(this.cbKurastBazaar)).addGroup(layout.createParallelGroup().addComponent(this.cbKurastUpper)).addGroup(layout.createParallelGroup().addComponent(this.cbTravincal)).addGroup(layout.createParallelGroup().addComponent(this.cbDuranceHate));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbKurastDocks, this.cbSpiderForest);
        layout.linkSize(0, this.cbKurastDocks, this.cbGreatMarsh);
        layout.linkSize(0, this.cbKurastDocks, this.cbFlayerJungle);
        layout.linkSize(0, this.cbKurastDocks, this.cbKurastLower);
        layout.linkSize(0, this.cbKurastDocks, this.cbKurastBazaar);
        layout.linkSize(0, this.cbKurastDocks, this.cbKurastUpper);
        layout.linkSize(0, this.cbKurastDocks, this.cbTravincal);
        layout.linkSize(0, this.cbKurastDocks, this.cbDuranceHate);
        layout.linkSize(1, this.cbKurastDocks, this.cbSpiderForest);
        layout.linkSize(1, this.cbKurastDocks, this.cbGreatMarsh);
        layout.linkSize(1, this.cbKurastDocks, this.cbFlayerJungle);
        layout.linkSize(1, this.cbKurastDocks, this.cbKurastLower);
        layout.linkSize(1, this.cbKurastDocks, this.cbKurastBazaar);
        layout.linkSize(1, this.cbKurastDocks, this.cbKurastUpper);
        layout.linkSize(1, this.cbKurastDocks, this.cbTravincal);
        layout.linkSize(1, this.cbKurastDocks, this.cbDuranceHate);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT3"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbKurastDocks == null) {
            (this.cbKurastDocks = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKurastDocks.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_KURAST_DOCKS"));
        this.cbKurastDocks.setFont(fntCheck);
        if (this.cbSpiderForest == null) {
            (this.cbSpiderForest = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSpiderForest.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_SPIDER_FOREST"));
        this.cbSpiderForest.setFont(fntCheck);
        if (this.cbGreatMarsh == null) {
            (this.cbGreatMarsh = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGreatMarsh.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_GREAT_MARSH"));
        this.cbGreatMarsh.setFont(fntCheck);
        if (this.cbFlayerJungle == null) {
            (this.cbFlayerJungle = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFlayerJungle.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_FLAYER_JUNGLE"));
        this.cbFlayerJungle.setFont(fntCheck);
        if (this.cbKurastLower == null) {
            (this.cbKurastLower = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKurastLower.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_KURAST_LOWER"));
        this.cbKurastLower.setFont(fntCheck);
        if (this.cbKurastBazaar == null) {
            (this.cbKurastBazaar = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKurastBazaar.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_KURAST_BAZAAR"));
        this.cbKurastBazaar.setFont(fntCheck);
        if (this.cbKurastUpper == null) {
            (this.cbKurastUpper = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKurastUpper.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_KURAST_UPPER"));
        this.cbKurastUpper.setFont(fntCheck);
        if (this.cbTravincal == null) {
            (this.cbTravincal = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTravincal.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_TRAVINCAL"));
        this.cbTravincal.setFont(fntCheck);
        if (this.cbDuranceHate == null) {
            (this.cbDuranceHate = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDuranceHate.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_03_DURANCE_HATE"));
        this.cbDuranceHate.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbKurastDocks.setSelected(selected);
        this.cbSpiderForest.setSelected(selected);
        this.cbGreatMarsh.setSelected(selected);
        this.cbFlayerJungle.setSelected(selected);
        this.cbKurastLower.setSelected(selected);
        this.cbKurastBazaar.setSelected(selected);
        this.cbKurastUpper.setSelected(selected);
        this.cbTravincal.setSelected(selected);
        this.cbDuranceHate.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbKurastDocks.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_DOCKS);
        }
        if (this.cbSpiderForest.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_SPIDER_FOREST);
        }
        if (this.cbGreatMarsh.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_GREAT_MARSH);
        }
        if (this.cbFlayerJungle.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_FLAYER_JUNGLE);
        }
        if (this.cbKurastLower.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_LOWER);
        }
        if (this.cbKurastBazaar.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_BAZAAR);
        }
        if (this.cbKurastUpper.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_UPPER);
        }
        if (this.cbTravincal.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_TRAVINCAL);
        }
        if (this.cbDuranceHate.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_03_DURANCE_HATE);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_DOCKS)) {
                this.cbKurastDocks.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_SPIDER_FOREST)) {
                this.cbSpiderForest.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_GREAT_MARSH)) {
                this.cbGreatMarsh.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_FLAYER_JUNGLE)) {
                this.cbFlayerJungle.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_LOWER)) {
                this.cbKurastLower.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_BAZAAR)) {
                this.cbKurastBazaar.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_KURAST_UPPER)) {
                this.cbKurastUpper.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_TRAVINCAL)) {
                this.cbTravincal.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_03_DURANCE_HATE)) {
                this.cbDuranceHate.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct3Pane.this.changed = true;
        }
    }
}
